package com.xstech.library.rxjavahttp;

import android.util.Log;


import com.xstech.library.app.BaseApp;
import com.xstech.library.utils.StringUtils;

import java.io.IOException;
import java.util.Set;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author: Allen.
 * @date: 2018/7/27
 * @description: 读取cookie
 */

public class CookieReadInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request.Builder builder = chain.request().newBuilder();

        Set<String> cookies=SharePreferencesUtils.getString(BaseApp.getApp(), "cookiess", null);
        if (cookies!=null){
            StringBuffer cookieBuf = new StringBuffer();
            for (String s:cookies){
               if (StringUtils.isEmpty(cookieBuf.toString())){

                   cookieBuf.append(s);
               }else {
                   cookieBuf.append(";"+s);
               }

            }

            builder.addHeader("Cookie",cookieBuf.toString() );
        }

        return chain.proceed(builder.build());
    }
}
